package com.cooba.activitys.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cooba.R;
import com.cooba.activitys.MainActivity;
import com.cooba.utils.Md5Utils;
import com.cooba.utils.ServerURL;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.text.SimpleDateFormat;
/**
 * Created by Administrator on 2015/12/12.
 * 注册
 */
public class RegisteredActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageButton mLoginimage_back;
    private EditText mEtNumber, mCheck_number, mEt_Password;
    private TextView mBt_getNumber, mTv_agreement, mRegisterbutton;
    private CheckBox mCb_Agree;


    private void bindViews() {

        mLoginimage_back = (ImageButton) findViewById(R.id.loginimage_back);
        mEtNumber = (EditText) findViewById(R.id.etNumber);
        mCheck_number = (EditText) findViewById(R.id.check_number);
        mEt_Password = (EditText) findViewById(R.id.et_Password);
        mBt_getNumber = (TextView) findViewById(R.id.bt_getNumber);
        mRegisterbutton = (Button) findViewById(R.id.registerbutton);
        mCb_Agree = (CheckBox) findViewById(R.id.cb_Agree);
        mTv_agreement = (TextView) findViewById(R.id.tv_agreement);

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);
        bindViews();
        mLoginimage_back.setOnClickListener(this);
        mBt_getNumber.setOnClickListener(this);
        mRegisterbutton.setOnClickListener(this);
        mTv_agreement.setOnClickListener(this);

        mCb_Agree.setOnClickListener(myAgreeListener);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        final HttpUtils httpUtils = new HttpUtils();
        final RequestParams params = new RequestParams();
        // TODO: 2015/12/10 获取系统时间
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String create_date = sDateFormat.format(new java.util.Date());
        //获取手机号
        final String phoneNumber = mEtNumber.getText().toString();

        //获取验证码
        final String codeNumber = mCheck_number.getText().toString();

        //获取密码
        String password = mEt_Password.getText().toString();
        final String md5password = Md5Utils.encodeBy32BitMD5(password);
        switch (v.getId()) {
            //返回
            case R.id.loginimage_back:
                intent = new Intent(RegisteredActivity.this, LoginActivity.class);
                startActivity(intent);
                break;

            //获取验证码
            case R.id.bt_getNumber:
                //关闭获取
                mBt_getNumber.setEnabled(false);
                //60S倒计时
                new CountDownTimer(60000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        mBt_getNumber.setText((millisUntilFinished / 1000) + "秒后重发");
                    }

                    @Override
                    public void onFinish() {
                        mBt_getNumber.setText("获取验证码");
                        mBt_getNumber.setEnabled(true);
                    }
                }.start();
                //先校验
                params.addBodyParameter("login_id", phoneNumber);
                httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.RegisterCheck, params, new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Log.e("sss", "验证码校验获取失败");
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String msg = JSON.parseObject(responseInfo.result).getString("msg");
                        String status = JSON.parseObject(responseInfo.result).getString("status");
                        Log.e("sss", "验证码校验获取成功");
                        if (status.equals("0")) {
                            Toast.makeText(getApplication(), phoneNumber + msg, Toast.LENGTH_SHORT).show();
                        } else {
                            // TODO: 2015/12/11  获取验证码
                            params.addBodyParameter("phone", phoneNumber);
                            httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.GetSendCode, params, new RequestCallBack<String>() {
                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    Log.e("sss", "获取验证码失败");
                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> responseInfo) {
                                    String msg = JSON.parseObject(responseInfo.result).getString("msg");
                                    String status = JSON.parseObject(responseInfo.result).getString("status");
                                    Log.e("ssssss", "验证码获取成功msg:" + msg + ">>status=" + status);
                                    if (status.equals("0")) {
                                        Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT).show();
                                    }
                                }

                            });
                        }
                    }
                });


                break;
            //提交
            case R.id.registerbutton:

                if (mCb_Agree.isChecked()) {
                    //输入判断
                    if (phoneNumber.length() > 0 && codeNumber.length() > 0 && password.length() > 0) {
                        params.addBodyParameter("phone", phoneNumber);
                        params.addBodyParameter("send_code", codeNumber);

                        httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.CheckSendCode, params, new RequestCallBack<String>() {
                            @Override
                            public void onFailure(HttpException arg0, String arg1) {
                                Log.e("sss", "获取失败");
                            }

                            @Override
                            public void onSuccess(ResponseInfo<String> responseInfo) {
                                String msg = JSON.parseObject(responseInfo.result).getString("msg");
                                String status = JSON.parseObject(responseInfo.result).getString("status");
                                Log.e("sss", "mima" + md5password + "status" + status + "--msg" + msg);
                                if (status.equals("1")) {
                                    params.addBodyParameter("login_id", phoneNumber);
                                    params.addBodyParameter("login_type", "1");
                                    params.addBodyParameter("password", md5password);
                                    params.addBodyParameter("name", "feng");
                                    params.addBodyParameter("create_date", create_date);
                                    params.addBodyParameter("phone", phoneNumber);
                                    httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.Register, params, new RequestCallBack<String>() {
                                        @Override
                                        public void onFailure(HttpException arg0, String arg1) {
                                            Log.e("sss", "获取失败");
                                        }

                                        @Override
                                        public void onSuccess(ResponseInfo<String> responseInfo) {
                                            String msg = JSON.parseObject(responseInfo.result).getString("msg");
                                            String status = JSON.parseObject(responseInfo.result).getString("status");
                                            Log.e("sss", "mima" + md5password + "status" + status + "--msg" + msg);
                                            if (status.equals("1")) {
                                                View view = LayoutInflater.from(getApplication()).inflate(R.layout.me_frament, null);

                                                TextView v = (TextView) view.findViewById(R.id.id_name);
                                                v.setText(phoneNumber);

                                                Intent intent = new Intent(RegisteredActivity.this, MainActivity.class);
                                                startActivity(intent);
                                                finish();
                                            }
                                        }
                                    });
                                } else {
                                    Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    } else {
                        if (phoneNumber.length() == 0) {
                            Toast.makeText(getApplication(), "请填写手机号", Toast.LENGTH_SHORT).show();
                        }
                        if (codeNumber.length() == 0) {
                            Toast.makeText(getApplication(), "请填写验证码", Toast.LENGTH_SHORT).show();
                        }
                        if (password.length() == 0) {
                            Toast.makeText(getApplication(), "请填写密码", Toast.LENGTH_SHORT).show();
                        }
                    }


                } else {
                    Toast.makeText(getApplication(), "请同意协议", Toast.LENGTH_SHORT).show();
                }


                break;
            //读取协议
            case R.id.tv_agreement:

                break;
        }
    }

    //判断是否同意协议
    public View.OnClickListener myAgreeListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mCb_Agree.isChecked()) {
                Toast.makeText(getApplication(), "同意协议", Toast.LENGTH_SHORT).show();
            }
        }
    };

}